<?php

namespace app\api\controller\share;

use app\common\controller\Frontend;
use app\logic\share\InviteLogic;
use app\api\validate\share\InviteValidate;

class Invite extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = [];

    protected InviteLogic $inviteLogic;

    public function initialize(): void
    {
        parent::initialize();

        $this->inviteLogic = new InviteLogic();

    }

    /**
     * 获取分享链接
     */
    public function getShareLink()
    {
        // 获取POST请求参数
        $params = $this->request->post();

        // 实例化验证器对象，用于后续的参数验证
        $validate = new InviteValidate();

        // 使用验证器的创建场景进行参数验证，如果验证失败则抛出异常
        try {
            $validate->scene('getShareLink')->check($params);
        } catch (\Throwable $e) {
            // 如果验证失败，显示错误消息
            $this->error($e->getMessage());
        }

        $res['note'] = [
            __('Invite Friends'),
            __('Easily promote and make money, 80% ofnetizens have made money'),
            __('Change'),
            __('Copy')
        ];
        $res['share_list'] = $this->inviteLogic->getShareLink($this->auth->id, $params['creator_user_id']);

        // 返回成功的消息
        $this->success(__('success'), $res);
    }

    /**
     * 绑定邀请码
     */
    public function bind()
    {
        // 获取POST请求参数
        $params = $this->request->post();

        // 实例化验证器对象，用于后续的参数验证
        $validate = new InviteValidate();

        // 使用验证器的创建场景进行参数验证，如果验证失败则抛出异常
        try {
            $validate->scene('bind')->check($params);
        } catch (\Throwable $e) {
            // 如果验证失败，显示错误消息
            $this->error($e->getMessage());
        }

        $res['note'] = [
            __('Want to chat with me'),
            __('Want to see my private photos andvideos'),
            __('Download it to your desktop, it will bemore convenient to find me in the future'),
            __('OK, Add to Desktop'),
            __('No, Go to Home Page')
        ];

        // 返回成功的消息
        $this->success(__('success'), $res);
    }
}